package com.elitel.frame.base.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 系统设置控制台
 * created by guoyanfei on 2018/4/9
 */
@Controller
@RequestMapping("/system/systemSetup")
public class SystemSetupController {
    private static final Logger logger = LoggerFactory.getLogger(SystemSetupController.class);
    private static final String strpath="WEB-INF\\classes";

    /**
     * 查询系统属性文件名称列表
     * created on 2018/04/09
     * @return 文件名称集合
     * @author guoyanfei
     */
    @RequestMapping("/getFileList")
    @ResponseBody
    public List<String> getFileList(HttpServletRequest request){
        List<String> filenamelist = new ArrayList<>();
        try{
            String path = request.getSession().getServletContext().getRealPath("");
            File file = new File(path+strpath);
            String[] filename = file.list();
            for (String fname: filename) {
                if(fname.lastIndexOf(".properties") !=-1){
                    filenamelist.add(fname);
                }
            }
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }

        return filenamelist;
    }

    /**
     * 通过文件名称查询文件内容
     * created on 2018/04/09
     * @param request
     * @return 文件内容
     * @author guoyanfei
     */
    @RequestMapping("/getFileContext")
    @ResponseBody
    public String getFileContext(HttpServletRequest request){
        String filecontext="";
        try{
            String path = request.getSession().getServletContext().getRealPath("");
            String filename = request.getParameter("filename");
            filecontext = getcontext(path+strpath+"\\"+filename);
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }
        return filecontext;
    }

    /**
     * 获取文件内容
     * created by guoyanfei on 2018/04/09
     * @return 文件内容
     */
    private String getcontext(String filepath){
        String fileContext="";
        FileInputStream fileInputStream =null;
        try {
            fileInputStream = new FileInputStream(filepath);
            //fis.available():文件的长度
            byte[] b=new byte[fileInputStream.available()];
            fileInputStream.read(b);
            fileContext = new String(b);
            fileInputStream.close();
        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }finally {
            try {
                fileInputStream.close();
            }catch (IOException e){
                logger.error(e.getMessage(),e);
            }
        }
        return fileContext;
    }

    /**
     * 保存修改的文件内容
     * created on 2018/04/09
     * @param request
     * @author guoyanfei
     */
    @RequestMapping("/saveFileContext")
    @ResponseBody
    public void saveFileContext(HttpServletRequest request){
        BufferedWriter bWriter = null;
        try{
            String path = request.getSession().getServletContext().getRealPath("");
            String filename = request.getParameter("filename");
            String newStr = request.getParameter("filecontext");

            bWriter = new BufferedWriter(new FileWriter(path+strpath+"\\"+filename,false));
            //写入文件
            bWriter.write(newStr);
            bWriter.newLine();
            bWriter.flush();

        }catch (Exception e){
            logger.error(e.getMessage(),e);
        }finally {
            try {
                bWriter.close();
            } catch (IOException e) {
                logger.error(e.getMessage(),e);
            }
        }
    }
}
